var img_manage = (function (){
	


	//当前页位置，当前页码。。。
	var currentPage,pagenow,pagesize,totalPage,count;
	var init = function(c,p,size,t,coun){
		currentPage = c;
		pagenow = p;
		pagesize = size;
		totalPage = t;
		count =coun;
		// updatePage();
		bindScrollEvent();




		// currentPage,pagenow,pagesize,totalPage,count;
		// console.log(currentPage,pagenow,pagesize,totalPage,count);
		


	};

	function updatePage(){
		$("#sp_grid_pager_records").text("共 " + count + " 张，共 " + totalPage + " 页，第 " + pagenow + " 页");
        $("#selPageNum").val(pagenow);



	}
var flat ;
	function bindScrollEvent(){
		flat = true;
		$(window).scroll(function(){  
		    	// console.log($(document).height() + '-'+$(this).scrollTop() +'-'+  $(this).height());
		    if ($(document).height() - $(this).scrollTop() - $(this).height() < 50){
		    	debugger;
				if (flat) {
		    		loadMore();  
		    	}else{
		    		return;
		    	}
		    } 
		});
	}

	function loadMore(){
		flat = false;
		loadImgData(parseInt(pagenow) + 1, pagesize,currentPage);
		
	}


	function loadImgData(p,pagesize,urlcode){
		$.ajax({
			// url:"{:U(Usercenter/index,array('code'=>'userlist'))}",
			url:Think.APP+"/Usercenter/index",
			type:"post",
			data:{'code':urlcode,pagesize:pagesize,p:p},
			beforeSend: function () {
			 	flag =0;					    
		        var loadHtm = '<div  class="loading-img col-xs-12" class="ui-widget-content jqgrow ui-row-ltr"><div class="col-xs-12" style="background-color:#fff"><img style="display:block;margin:0 auto;height:100px;" src="/Public/home/img/load-img.gif" alt="" /></div></div>';
		        $('#img_content').append(loadHtm);
		    },
			success:function(data){
				count = data.page.count;
				totalPage = data.page.pagecount;
				pagenow = data.page.page;
				// updatePage();
				var pageRows = data.list;
				debugger;
				updateImgContent(pageRows);
				$("#imgContainer .sel_img_box input[type='checkbox']").change(function(event) {
					debugger;
					if(editFlat === true){
						checkFlat = true;
						var objArr = $("#imgContainer .sel_img_box input[type='checkbox']");
							for(var i = 0; i<objArr.length; i++){
								if(objArr[i].checked === false){
									checkFlat = false;
									$("#checkImgAll").find("span.text").text("全选");
									$("#checkImgAll").find("span.glyphicon").removeClass('glyphicon-remove');
									$("#checkImgAll").find("span.glyphicon").addClass('glyphicon-ok');
									// return false
								}else{
									checkArr.push(objArr[i]);
								}
							}
					}
					if(checkFlat === true){
						if($.trim($("#checkImgAll").text()) === "全选"){
							$("#checkImgAll").trigger("click")
						}
						
					}
				});
				if(editFlat === true){
					$("#editImgButton").find("span.text").text("编辑");
					$("#editImgButton").trigger('click');	

				}
				// var idArr = [];

				debugger;
				for(var i = 0 ; i<checkArr.length;i++){
					// idArr.push(checkArr[i].id)

					$("#"+checkArr[i].id).prop("checked",true);
				}



				


			
				// $("#page-content").
				// $("#page-content").html(data);
			},
			complete:function(){
				$(".loading-img").remove();	
			},
			error:function(){
				console.log("网络错误")
				/* Act on the event */
			}
		})
	}


	function updateImgContent(data){
		var html = "";
		for(var i = 0; i<data.length;i++){
			html += '<div class="col-xs-3 img_box">';
			html += '<span class="sel_img_box hide">';
			html += '<input class="edit_img" id="'+data[i].id+'check" disabled="disabled" type="checkbox">';
			html += '</span>';
			html += '<label for="'+data[i].id+'check" href="#" class="thumbnail">';		
			html += '<img  data-src="" src="'+Think.PUBLIC+data[i].compress_url+'" alt="'+data[i].addtime+'">'		    
			html += '</label></div>';			     			  
		}
		$("#img_content").append(html);
		flat = true;
	}



	function toPage(flag) {
	    debugger;
	    if(parseInt(totalPage) === 1 && parseInt(pagenow) === 1){
	       alert("-.- NO MORE");
	        return false;
	    }
	    switch (flag) {
	        case "first":
	            if(parseInt(pagenow) === 1){
	                alert("已经是第一页了");
	            }else{
	                loadImgData(1, pagesize,currentPage);
	            }
	            break;
	        case "prev":
	            if (pagenow > 1) {
	                loadImgData(parseInt(pagenow) - 1, pagesize,currentPage);
	            }else{
	                alert("没有上一页了 - . -");
	            }
	            break;
	        case "next":
	            if (pagenow < totalPage) {
	                loadImgData(parseInt(pagenow) + 1, pagesize,currentPage);
	            }else{
	                alert("没有下一页了 - . -");
	            }
	            break;
	        case "last":
	            if (pagenow < totalPage) {
	                loadImgData(totalPage, pagesize,currentPage);
	            }else{
	               alert("已经是最后一页了")
	            }
	            break;
	        case "num":
	            var num = $("#selPageNum").val();
	            if(parseFloat(num) === parseFloat(pagenow) || isNaN(num) || parseFloat(num)>totalPage){
	                alert("请输入当前页除外且不大于总页数的正整数");
	                return false;
	            }else {
	                loadImgData(num,pagesize,currentPage);
	            }
	    }

	    return false;
	}

	

	return {
		Init:init,
		ToPage:toPage
	};


})();